package Eclass.Question;

import java.sql.SQLException;
import java.util.List;

import ibatis.IbatisHelper;

import Forum.ForumThreadModel;

import com.ibatis.sqlmap.client.SqlMapClient;

public class QuestionManager {
	private SqlMapClient ibatis;
		
	public QuestionManager() {
		this.ibatis = IbatisHelper.getSqlMapInstance();
	}
	
	public List selectQuestion() {
		List result = null;
		try {
		result = ibatis.queryForList("question.getListQuestion","");
		} catch (SQLException e) {
		e.printStackTrace();
		}
		return result;
		}
	
	public List selectClass() {
		List result = null;
		try {
			result = ibatis.queryForList("question.getListClass","");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public QuestionModel selectUpdate(String id) {
		QuestionModel bean = new QuestionModel();
		try {
		bean = (QuestionModel) ibatis.queryForObject("question.getQuestion",id);
		} catch (SQLException e) {
		e.printStackTrace();
		}
		return bean;
		}
	
	public boolean addQuestion(QuestionModel bean) {
		boolean result = true;
		
		try {
			ibatis.startTransaction();
			if(bean.getExamFor().toString().equalsIgnoreCase("trainee")) {
				this.ibatis.insert("question.addQuestionTrainee", bean);
			} else if(bean.getExamFor().toString().equalsIgnoreCase("candidate")) {
				this.ibatis.insert("question.addQuestionCandidate", bean);
			}
			ibatis.commitTransaction();
		} catch (SQLException sqe) {
			// TODO: handle exception
			sqe.printStackTrace();
			result = false;
		} finally {
			try {
				ibatis.endTransaction();
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		return result;
	}
	
	 public boolean deleteQuestion(String id) {
		 boolean result = true;
		 
		 try {
				 ibatis.startTransaction();
				 this.ibatis.insert("question.deleteQuestion", id);
				 ibatis.commitTransaction();
			 } catch (SQLException sqe) {
				 // TODO: handle exception
				 sqe.printStackTrace();
				 result = false;
			 } finally {
				 try {
				 ibatis.endTransaction();
				 } catch (Exception e) {
					 // TODO: handle exception
					 e.printStackTrace();
				}
			 }
		 return result;
	 }
	 
	 public boolean updateQuestion(QuestionModel bean) {
		 boolean result = true;
		 
		 try {
				 ibatis.startTransaction();
				 if(bean.getExamFor().toString().equalsIgnoreCase("trainee")) {
					 this.ibatis.insert("question.updateQuestionTrainee", bean);
				 } else if(bean.getExamFor().toString().equalsIgnoreCase("candidate")) {
					 this.ibatis.insert("question.updateQuestionCandidate", bean);
				 }
				 ibatis.commitTransaction();
			 } catch (SQLException sqe) {
				 // TODO: handle exception
				 sqe.printStackTrace();
				 result = false;
			 } finally {
				 try {
					 ibatis.endTransaction();
				 } catch (Exception e) {
					 // TODO: handle exception
					 e.printStackTrace();
				 }
			 }
		 
		 return result;
		 }
 
}
